<?php
use app\common\model\System;
use app\common\model\UserSection;

/**
 * 打印
 */
function ff($data)
{
    print_r($data);die;
}

/** 
 * 密码加密 
 */
function setPass($pass, $salt)
{
    $pass = "#>_<#" . md5(md5($salt . $pass));
    return $pass;
}

/**
 * 获取当前域名
 */
function getUrl()
{
    return Request()->domain(1);
}

/**
 * 文件上传
 */
function fileUp($file, $path = 'image')
{
    $name = \think\facade\Filesystem::disk('public')->putFile($path, $file);
    $filePath = str_replace('\\','/',$name);
    return '/storage/'.$filePath;
}

/** 
 * 订单号 
 */
function getOrderNo()
{
    return date('Ymd'). str_pad(mt_rand(1, 99999), 5, 0, STR_PAD_LEFT);
}

/**
 * 模拟get请求
*/
function curl_get($url)
{
    $headers = array('User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36');
    $oCurl = curl_init();

    if (stripos($url, "https://") !== FALSE) {
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
    }

    curl_setopt($oCurl, CURLOPT_TIMEOUT, 20);
    curl_setopt($oCurl, CURLOPT_URL, $url);
    curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
    $sContent = curl_exec($oCurl);
    $aStatus = curl_getinfo($oCurl);
    curl_close($oCurl);

    if (intval($aStatus["http_code"]) == 200) {
        return $sContent;
    } else {
        return false;
    }
}

/**
 * 模拟post请求
 */
function curl_post($url, $data = array())
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    // POST数据
    curl_setopt($ch, CURLOPT_POST, 1);
    // 把post的变量加上
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}

/**
 * 获取请求IP
 */
function getIP()
{
    if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")){
        $ip = getenv("HTTP_CLIENT_IP");
    }else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")){
        $ip = getenv("REMOTE_ADDR");
    }else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")){
        $ip = $_SERVER['REMOTE_ADDR'];
    }else{
        $ip = "unknown";
    }
    
    return $ip;
}

/**
 * 秒转换小时（时分格式）
 */
function secondToHourStr($second)
{
    $newtime = '';

    $h = floor(($second % (3600*24)) / 3600);
    $m = floor((($second % (3600*24)) % 3600) / 60);

    if($h != 0){
        if($h < 10) $h = $h . '小时';

        if($m == 0){
            $newtime = $h;
        }else{
            $newtime = $h . '小时' . $m . '分钟';
        }
    }else{
        $newtime = $m . '分钟';
    }

    return $newtime;
}

/**
 * 秒转换小时（时间格式）
 */
function secondToHour($second)
{
    $newtime = '';

    $h = floor(($second % (3600*24)) / 3600);
    $m = floor((($second % (3600*24)) % 3600) / 60);

    if($h != 0){
        if($h < 10) $h = '0' . $h;

        if($m == 0){
            $newtime = $h . ':00';
        }else{
            $newtime = $h . ':' . $m;
        }
    }else{
        $newtime = '00:' . $m;
    }

    return $newtime;
}

/**
 * 秒转换时分秒（1时28分32秒）
 */
function secondToHour2($second)
{
    $newtime = '';

    $h = floor($second / 3600);
    $m = floor(($second % 3600) / 60);
    $s = ($second % 3600) % 60;

    if($s != 0 || $m != 0 || $h != 0){
        if($h != 0) $newtime .= $h . '时';
        if($m != 0) $newtime .= $m . '分';
        if($s != 0) $newtime .= $s . '秒';
    }

    return $newtime;
}

/**
 * 秒转换小时（小数格式）
 */
function secondToHours($second = 0)
{
    return rounding($second / 3600, 2);
}

/**
 * 小时转换秒（时间格式）
 */
function hourToSecond($time)
{
    list($hour, $i) = explode(':', $time);
    return $hour * 3600 + $i * 60;
}

/**
 * 年份格式化
 */
function setTimeYear($time = 0)
{
    if($time <= 0) return $time;
    $month = ceil(ceil((time() - $time) / 86400) / 30);

    if($month < 12){
        return $month . '个月';
    }else if($month % 12 == 0){
        return $month / 12 . '年';
    }else if($month > 12){
        return floor($month / 12) . '年' . $month % 12 . '个月';
    }
}

/**
 * 系统参数
 */
function getSystem()
{
    return System::getFind(['id' => 1]);
}

/** 随机字符串 */
function getRandStr($length = 8)
{
    $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
    $len = strlen($str) - 1;

    $randstr = '';
    for($i = 0; $i < $length; $i++){
        $num = mt_rand(0, $len);
        $randstr .= $str[$num];
    }

    return $randstr;
}

/**
 * 生成工号
 */
function getStaffNo($sectionId = 0)
{
    $info = UserSection::getFind(['id' => $sectionId]);
    if($info === false) return '';
    return $info['serial_no'] . sprintf('%06s', $info['nums']);
}

/**
 * 保留N位小数
 */
function rounding($num, $i = 2)
{
    $s = pow(10, $i);
    return intval($num * $s) / $s;
}

/**
 * 薪资转换
 */
function setSalary($salary)
{
    if($salary < 10000) return $salary;

    $salaryNew = $salary / 1000;

    return $salaryNew . 'K';
}

/**
 * 计算年龄
 */
function getAge($birthday)
{
    /** 格式化出生时间年月日 */
    $byear  = date('Y', $birthday);
    $bmonth = date('m', $birthday);
    $bday   = date('d', $birthday);

    /** 格式化当前时间年月日 */
    $tyear = date('Y');
    $tmonth = date('m');
    $tday = date('d');

    /** 开始计算年龄 */
    $age = $tyear - $byear;
    if ($bmonth > $tmonth || $bmonth == $tmonth && $bday > $tday) {
        $age--;
    }
    
    return $age;
}

/**
 * 表格表头字母
 */
function getLetter($length)
{
    $arr = [
        'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'z', 
        'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
    
    return array_slice($arr, 0, $length);
}

/**
 * 文件大小转换
 */
function fileSizeTrans($byte = 0)
{
    if(!$byte) return '0M';
    if($byte < 1024) return $byte . 'B';
    $byteK = rounding($byte / 1024);
    if($byteK < 1024) return $byteK . 'KB';
    return rounding($byte / 1024 / 1024) . 'M';
}